package org.tloss.cluster;

import java.io.StringWriter;
import java.util.Enumeration;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionContext;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

public class HttpSessionWrapper implements HttpSession {
	HttpSession orig;
	HttpServletRequest request;
	HttpServletResponse response;

	public HttpSessionWrapper(HttpSession session, HttpServletRequest request,
			HttpServletResponse response) {
		this.orig = session;
		this.request = request;
		this.response = response;

	}

	public Object getAttribute(String arg0) {
		return orig.getAttribute(arg0);
	}

	public Enumeration getAttributeNames() {
		return orig.getAttributeNames();
	}

	public long getCreationTime() {

		return orig.getCreationTime();
	}

	public String getId() {
		return orig.getId();
	}

	public long getLastAccessedTime() {
		return orig.getLastAccessedTime();
	}

	public int getMaxInactiveInterval() {
		return orig.getMaxInactiveInterval();
	}

	public ServletContext getServletContext() {

		return orig.getServletContext();
	}

	public HttpSessionContext getSessionContext() {
		return orig.getSessionContext();
	}

	public Object getValue(String arg0) {

		return orig.getValue(arg0);
	}

	public String[] getValueNames() {

		return orig.getValueNames();
	}

	public void invalidate() {
		orig.invalidate();

	}

	public boolean isNew() {

		return orig.isNew();
	}

	public void putValue(String arg0, Object arg1) {
		orig.putValue(arg0, arg1);

	}

	public void removeAttribute(String arg0) {
		String name = request.getHeader(Constants.SERVER_CLUSTER_NAME);
		StringBuffer buffer = new StringBuffer();
		buffer.append(name).append("=").append(arg0);
		if (!response.isCommitted()) {
			response.setHeader(Constants.DELETE_SESSION, buffer.toString());
		}
		orig.removeAttribute(arg0);
	}

	public void removeValue(String arg0) {
		orig.removeValue(arg0);

	}

	public void setAttribute(String arg0, Object arg1) {
		Marshaller jaxbMarshaller = (Marshaller) getServletContext()
				.getAttribute(Constants.MARSHALLER);
		if (arg1 != null) {
			if (jaxbMarshaller != null) {
				try {
					SessionMeta src = new SessionMeta();
					String name = request
							.getHeader(Constants.SERVER_CLUSTER_NAME);
					src.setClusterName(name);
					src.setSessionKey(arg0);
					JAXBContext context;

					context = JAXBContext.newInstance(arg1.getClass());

					StringWriter writer = new StringWriter();
					Marshaller marshaller = context.createMarshaller();
					marshaller.setProperty(Marshaller.JAXB_FRAGMENT,
							Boolean.TRUE);
					marshaller.marshal(arg1, writer);
					src.setSessionValue(writer.toString());
					src.setSessionValueClass(arg1.getClass().getName());
					if (!response.isCommitted()) {
						StringWriter handler = new StringWriter();
						jaxbMarshaller.marshal(src, handler);
						response.setHeader(Constants.ADD_SESSION,
								handler.toString());
					}
				} catch (JAXBException e) {

				}
			}
			orig.setAttribute(arg0, arg1);
		} else {
			removeAttribute(arg0);
		}

	}

	public void setMaxInactiveInterval(int arg0) {
		orig.setMaxInactiveInterval(arg0);

	}

}
